"""
@author: 江同学呀
@file: dates_field.py
@date: 2024/7/26 11:19
@desc:
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/date.html
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/date_nanos.html

    Date types, including date and date_nanos.
    日期类型，包括 date 和 date_nanos 。
"""
import datetime
from abc import ABC
from typing import Type, Union, Dict, Any, List

from espc.common.field_common import TimeFormat
from espc.orm.model.base.base import _Base
from espc.orm.model.mapping.field.base_field.base_time_field import _BaseTimeField
from espc.orm.model.mapping.field.field_attribute import (
    DocValuesAttribute, BoostAttribute, IndexAttribute, NullValueAttribute, FormatAttribute, IgnoreMalformedAttribute,
    LocaleAttribute, OnScriptErrorAttribute, ScriptAttribute, StoreAttribute, MetaAttribute)
from espc.utils.build_add_attr import build_add_attr
from espc.utils.enum_utils import EnumMerge


class DateField(
    _BaseTimeField, BoostAttribute, DocValuesAttribute, FormatAttribute, LocaleAttribute, IgnoreMalformedAttribute,
    IndexAttribute, NullValueAttribute, OnScriptErrorAttribute, ScriptAttribute, StoreAttribute,
    MetaAttribute, ABC
):
    """
    日期 字段
    """
    _field_type: str = "date"

    def __init__(
            self, field_name: str = None, value: datetime.datetime = None, default: datetime.datetime = None,
            boost: float = None, doc_values: bool = None,
            format_: Union[str, TimeFormat, List[Union[str, TimeFormat]], EnumMerge] = None, locale: str = None,
            ignore_malformed: bool = None, index: bool = None, null_value: Any = None, on_script_error: str = None,
            script: str = None, store: bool = None, meta: Dict = None,
    ):
        super().__init__(
            field_name=field_name, value=value, default=default, boost=boost, doc_values=doc_values, format_=format_,
            locale=locale, ignore_malformed=ignore_malformed, index=index, null_value=null_value,
            on_script_error=on_script_error, script=script, store=store, meta=meta
        )
        return

    def __get__(self, instance: _Base, owner: Type["DateField"]) -> Union["DateField", datetime.datetime, str]:
        return super(DateField, self).__get__(instance=instance, owner=owner)

    def __set__(self, instance, value: Union[str, datetime.datetime]):
        super(DateField, self).__set__(instance=instance, value=value)
        return

    @property
    def value(self) -> datetime.datetime:
        return self._value

    @value.setter
    def value(self, value: datetime.datetime):
        self._value: datetime.datetime = value
        return

    @build_add_attr
    def _build(self) -> Dict:
        mapping: Dict = {
            "type": self._field_type
        }
        return mapping


class DateNanosField(
    _BaseTimeField, BoostAttribute, DocValuesAttribute, FormatAttribute, LocaleAttribute, IgnoreMalformedAttribute,
    IndexAttribute, NullValueAttribute, OnScriptErrorAttribute, ScriptAttribute, StoreAttribute,
    MetaAttribute, ABC
):
    """
    日期纳秒 字段
    """
    _field_type: str = "date_nanos"

    def __init__(
            self, field_name: str = None, value: Union[datetime.datetime, str] = None,
            default: Union[datetime.datetime, str] = None, boost: float = None,
            format_: Union[str, TimeFormat, List[Union[str, TimeFormat]]] = None, doc_values: bool = None,
            locale: str = None, ignore_malformed: bool = None, null_value: Any = None, on_script_error: str = None,
            script: str = None, store: bool = None, index: bool = None, meta: Dict = None,
    ):
        super().__init__(
            field_name=field_name, value=value, default=default, boost=boost, doc_values=doc_values, format_=format_,
            locale=locale, ignore_malformed=ignore_malformed, index=index, null_value=null_value,
            on_script_error=on_script_error, script=script, store=store, meta=meta
        )
        return

    def __get__(
            self, instance: _Base, owner: Type["DateNanosField"]
    ) -> Union["DateNanosField", datetime.datetime, str]:
        return super(DateNanosField, self).__get__(instance=instance, owner=owner)

    def __set__(self, instance, value: Union[str, datetime.datetime]):
        super(DateNanosField, self).__set__(instance=instance, value=value)
        return

    @property
    def value(self) -> Union[datetime.datetime, str]:
        return self._value

    @value.setter
    def value(self, value: Union[datetime.datetime, str]):
        self._value: Union[datetime.datetime, str] = value
        return

    @build_add_attr
    def _build(self) -> Dict:
        mapping: Dict = {
            "type": self._field_type
        }
        return mapping







